import layoutMain from "../layout/index";

let files = require.context('@/views', false, /\.vue$/)

let pages = {}
let routes = []
let config = {
    layoutMain
}


files.keys().forEach(key => {
    pages[key.replace(/(\.\/|\.vue)/g, "")] = files(key).default
})


Object.keys(pages).forEach(item => {
    let props=pages[item].props
    if(pages[item].name){
        if(props && props.length>0){
            routes.push(renderRoute(pages,item,props))
        }else{
            routes.push(renderRoute(pages,item))
        }
    }else{
        console.warn('没有定义name属性')
    }

})

function renderRoute(pages, item, props) {
    let route = {
        path: `/${pages[item].name}`
    }
    if(props){
        route.path=`/${pages[item].name}/:${props.join('/:')}`
        route.props=true
    }

    // console.log(route);
    if (pages[item].layout) {

        route.component = config[pages[item].layout]
        route.children = [
            {
                path: '/',
                component: pages[item],
                name: pages[item].name

            }
        ]
    } else {
        // route.name = pages[item].name
        route.component = pages[item]
    }
    return route
}
console.log(routes)
export default  routes